﻿#include "imageutils.h"
#include <QImage>
#include <QDateTime>
#include <QDir>
#include <QDebug>

bool ImageUtils::saveImageFromData(const QByteArray &data,
                                 const QString &outputDir,
                                 const QString &ext,
                                 int pageNumber) {
    // return true;
    QImage image;
    if (!image.loadFromData(reinterpret_cast<const uchar*>(data.data()),
                          static_cast<int>(data.size()))) {
        // qWarning() << "Failed to load image from data";
        return false;
    }

    QDir().mkpath(outputDir);
    QString imageFileName = QString("%1/image_%2_%3.%4")
                          .arg(outputDir)
                          .arg(pageNumber)
                          .arg(QDateTime::currentMSecsSinceEpoch())
                          .arg(ext);

    if (!image.save(imageFileName)) {
        // qWarning() << "Failed to save image" << imageFileName;
        return false;
    }
   qDebug() << "Saved image to" << imageFileName;
    return true;
}
